package SymbolTable;

require Exporter;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use IssDump ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
    Sym_Load
    Sym_SymToAddr
    Sym_AddrToSym
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
	
);

my %Gsymbols = ();

sub Sym_Load {
  my $path = shift;
  my $fh = "";
  if( ! open ($fh, "<", $path) ) {
    warn "Cannot open $path: $!\n";
    return 0;
  }
  while(<$fh>) {
    chomp;
    @line = split / +/;
    $Gsymbols{$line[0]} = hex($line[1]);
  }
  close $fh;
  return 1;
}

sub Sym_SymToAddr {
  my $name = shift;
  my $addr = 0;
  if(exists $Gsymbols{$name}) {
    $addr = $Gsymbols{$name};
  }
  return $addr;
}

sub Sym_AddrToSym {
  my $addr = shift;
  my $offset = 0xffffffff;
  my $ret = undef;
  for my $name (keys %Gsymbols) {
    my $value = $Gsymbols{$name};
    next if( $addr < $value );
    if( $addr - $value < $offset ) {
      $offset = $addr - $value;
      $ret = $name;
    }
  }
  return sprintf("$ret+0x%x", $offset);
}

1;
